<?php

if (!function_exists('resolveView')) {
    function resolveView($class = __CLASS__, $function = __FUNCTION__, $id = '')
    {
        $pattern = '/(?:.*[\/|\\\\])?(.*)([A-Z].*)/';
        $folder = preg_replace($pattern, '$1', $class);

        if ($class == $function) {
            $template = $folder;
        } else {
            $template = preg_replace($pattern, '$2', $function);
        }

        return strtolower($folder . ($template ? '.' . $template : '') . ($id ? '_' . $id : ''));
    }
}

if (!function_exists('vw')) {
    function vw($class = '', $function = '', $id = '')
    {
        if (!$class || !$function) {
            if (!$class) {
                $class = $function;
            } else {
                $function = $class;
            }
        }

        foreach (debug_backtrace() as $trace) {
            if (isset($trace['class']) && isset($trace['function'])) {
                $class = $trace['class'];
                $function = $trace['function'];
                break;
            }
        }

        return resolveView($class, $function);
    }
}

if (!function_exists('initCliEnv')) {
    function initCliEnv()
    {
        if (PHP_SAPI === 'cli' && config('database.connections.mysql.host') != '127.0.0.1') {
            config()->set('database.connections.mysql.host', '127.0.0.1');
        }
    }
}
